package edu.ccut.saturn.studio.naviagtor.wizard.project;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.core.exception.XMLException;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;

public class DefaultGenerateFiles {

	private static final String FILE_ATTRIBUTE_TEMPLATE = "template";

	private static final String FILE_ATTRIBUTE_DIR = "dir";

	private static final String FILE_ATTRIBUTE_NAME = "name";

	private static final String ELEMENT_FILE = "file";

	private static List<FileInfo> fileInfos = new ArrayList<FileInfo>();

	private static final String GENERATE_FILE = "generatefile.xml";

	static {
		loadFileInfos();
	}

	public static void generateDirectory(String projectName) {
		String separator = File.separator;
		String projectPath = ResourcesPlugin.getWorkspace().getRoot()
				.getLocation().toOSString()
				+ separator + projectName;

		new Path(projectPath).toFile().mkdir();

		String srcFolder = projectPath + separator + "src" + separator; //$NON-NLS-1$
		new Path(srcFolder).toFile().mkdir();

		String actFolder = srcFolder + separator
				+ projectName.replace(".", separator) + separator; //$NON-NLS-1$ 
		new Path(actFolder).toFile().mkdir();

		String libPath = projectPath + separator + "lib" + separator; //$NON-NLS-1$
		new Path(libPath).toFile().mkdir();

		String metaPath = projectPath + separator + "META-INF" + separator; //$NON-NLS-1$
		new Path(metaPath).toFile().mkdir();
	}

	public static void generateFile(String projectPath,
			Map<String, String> values) {
		
		for (FileInfo fileInfo : fileInfos) {
			String name = fileInfo.getName();
			String dir = getDir(fileInfo.getDir(), values);

			String template = fileInfo.getTemplate();

			String filePath = projectPath + File.separator + dir + name;

			FreemarkerUtils.writeTemplateToFile(template, values, filePath);
		}
	}

	private static void loadFileInfos() {
		InputStream input = DefaultGenerateFiles.class
				.getResourceAsStream(GENERATE_FILE);

		try {
			Document doc = SaturnW3CUtil.getDocument(input);
			Node root = doc.getDocumentElement();

			List<Node> fileNodes = SaturnW3CUtil.getChildrenByName(root,
					ELEMENT_FILE);

			if (fileNodes != null) {
				for (Node fileNode : fileNodes) {
					String name = SaturnW3CUtil.getNodeAttributeValue(fileNode,
							FILE_ATTRIBUTE_NAME);
					String dir = SaturnW3CUtil.getNodeAttributeValue(fileNode,
							FILE_ATTRIBUTE_DIR);
					String template = SaturnW3CUtil.getNodeAttributeValue(
							fileNode, FILE_ATTRIBUTE_TEMPLATE);

					FileInfo fileInfo = new FileInfo();
					fileInfo.setName(name);
					fileInfo.setDir(dir);
					fileInfo.setTemplate(template);

					fileInfos.add(fileInfo);
				}
			}
		} catch (XMLException e) {
			e.printStackTrace();
		}
	}

	private static String getDir(String dir, Map<String, String> values) {
		String temp = dir;

		if (dir == null) {
			return "";
		}

		if (dir.indexOf("${") >= 0) {
			for (String key : values.keySet()) {
				temp = temp.replace("${" + key + "}", values.get(key));
			}
		}

		if (!"".equals(dir.trim())) {
			temp += File.separator;
		}

		return temp;
	}

	private static class FileInfo {
		private String name;
		private String dir;
		private String template;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getDir() {
			return dir;
		}

		public void setDir(String dir) {
			this.dir = dir;
		}

		public String getTemplate() {
			return template;
		}

		public void setTemplate(String template) {
			this.template = template;
		}
	}
}
